/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2015
 */


package com.environment_pro.monitor.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.environment_pro.monitor.model.TbDeviceMsg;
import com.environment_pro.monitor.service.TbDeviceMsgService;
import com.environment_pro.monitor.util.StaticUtil;
import com.environment_pro.monitor.util.PageHelper;
import com.environment_pro.monitor.util.PageHelper.Page;

@Controller
@RequestMapping("/tbDeviceMsg")
public class TbDeviceMsgController extends BaseController {
	
	private static final Logger logger = Logger.getLogger(TbDeviceMsgController.class);

	@Autowired
	private TbDeviceMsgService tbDeviceMsgService;
	
	/**
	 * 获取所有列表 
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAllTbDeviceMsg")
	public String getAllTbDeviceMsg(TbDeviceMsg tbDeviceMsg, HttpServletRequest request){
		//初始化分页 参数
		initPageParameter(request);
		//查询
		Page<TbDeviceMsg> mypage = tbDeviceMsgService.findAll(pageInfo,tbDeviceMsg);
		//返回查询条件和结果
		request.setAttribute("mypage", mypage);
		request.setAttribute("tbDeviceMsg", tbDeviceMsg);
		request.setAttribute("tbDeviceMsgList", mypage.getResult());
		//返回页面
		return "/TbDeviceMsg/allTbDeviceMsg";
	}
	
	/**
	 * 获取所有列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAllTbDeviceMsgJson")
	@ResponseBody
	public List<TbDeviceMsg> getAllTbDeviceMsgJson(TbDeviceMsg tbDeviceMsg,HttpServletRequest request){
		//初始化分页 参数
		initPageParameter(request);
		Page<TbDeviceMsg> mypage = tbDeviceMsgService.findAll(pageInfo,tbDeviceMsg);
		
		return mypage.getResult();
	}
	
	/**
	 * 跳转到添加界面
	 * @param request
	 * @return
	 */
	@RequestMapping("/toAddTbDeviceMsg")
	public String toAddTbDeviceMsg(HttpServletRequest request){
		
		return "/TbDeviceMsg/addTbDeviceMsg";
	}
	/**
	 * 添加并重定向
	 * @param tbDeviceMsg
	 * @param request
	 * @return
	 */
	@RequestMapping("/addTbDeviceMsg")
	public String addTbDeviceMsg(TbDeviceMsg tbDeviceMsg,HttpServletRequest request){
		tbDeviceMsgService.save(tbDeviceMsg);
		this.saveLogOperate(request,"");
		logger.info("");
		return "redirect:/tbDeviceMsg/getAllTbDeviceMsg";
	}
	
	/**
	 *编辑
	 * @param tbDeviceMsg
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateTbDeviceMsg")
	public String updateTbDeviceMsg(TbDeviceMsg tbDeviceMsg,HttpServletRequest request){
		
		if(tbDeviceMsgService.update(tbDeviceMsg)){
			//tbDeviceMsg = tbDeviceMsgService.findById(tbDeviceMsg.getId());
			request.setAttribute("tbDeviceMsg", tbDeviceMsg);
			this.saveLogOperate(request,"");
			return "redirect:/tbDeviceMsg/getAllTbDeviceMsg";
		}else{
			return "/error";
		}
	}
	/**
	 * 根据id查询单个
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/getTbDeviceMsg")
	public String getTbDeviceMsg(int id,HttpServletRequest request){
		
		request.setAttribute("tbDeviceMsg", tbDeviceMsgService.findById(id));
		return "/TbDeviceMsg/editTbDeviceMsg";
	}
	
	/**
	 * 根据id查询单个
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/getTbDeviceMsgJson")
	@ResponseBody
	public TbDeviceMsg getTbDeviceMsgJson(int id,HttpServletRequest request){
		return tbDeviceMsgService.findById(id);
	}
	
	/**
	 * 删除
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delTbDeviceMsg")
	public void delTbDeviceMsg(int id,HttpServletRequest request,HttpServletResponse response){
		String result = "{\"result\":\"error\"}";
		TbDeviceMsg tbDeviceMsg = tbDeviceMsgService.findById(id);
		if(tbDeviceMsgService.delete(id)){
			result = "{\"result\":\"success\"}";
		}
		this.saveLogOperate(request,tbDeviceMsg.toString());
		response.setContentType("application/json");
		this.saveLogOperate(request,"");
		try {
			response.getWriter().write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

